package com.glebusheg  
{
	import com.glebusheg.app.SuitApp;
	import com.glebusheg.asset.AssetLoader;
	import com.glebusheg.asset.AssetManager;
	import com.glebusheg.client.RunMode;
	import com.glebusheg.data.AppData;
	import com.glebusheg.debug.Console;
	import com.glebusheg.text.StringManager;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.system.Security;

	//import com.flashdynamix.utils.SWFProfiler;
	/**
	 * ...
	 * @author Gleb Volkov
	 */
	[SWF(backgroundColor="#000000", width="800", height="600", frameRate="30")]
	public class AppMain extends Sprite 
	{		
		private var _descriptionLoader:AssetLoader;
		private var _assetLoader:AssetLoader;
		
		private var _console:Console;
		
		private static const ASSETS:Object = 
		{
			"asset":"asset.xml"
		};
		
		public function AppMain() 
		{
			//SWFProfiler.init(stage, this);	
			_console = Console.instance; 
			addChild(_console);
			
			AssetManager.initialize();
			
			var fv:Object = stage.loaderInfo.parameters;
			if(fv.hasOwnProperty("version"))
				AssetManager.antiCacheVersion = fv["version"];
			AssetManager.assetURL = (fv.hasOwnProperty("asset_url")) ? fv["asset_url"] : "asset";
			
			RunMode.d(this);	
			Console.log("Run mode : " + RunMode.current);
			
			Security.allowDomain("*");
			AppFonts.initialize();
			
			for (var name:String in ASSETS)
				AssetManager.add(name, ASSETS[name]);
			
			var locale:String = "ru";
			if(fv.hasOwnProperty("locale"))
				locale = fv.locale;
			
			AssetManager.add("locale", "data/locale_" + locale + ".xml");
			
			initialize();
			
			_descriptionLoader = AssetManager.load();
			_descriptionLoader.addEventListener(Event.COMPLETE, onDescLoaded);
			_descriptionLoader.load();
		}
		
		private function initialize():void 
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.stageFocusRect = false;
			stage.tabChildren = false;
			
			// context menu handling
		}
		
		private function onDescLoaded(e:Event):void 
		{
			_descriptionLoader.removeEventListener(Event.COMPLETE, onDescLoaded);
			_descriptionLoader = null;
			
			for (var name:String in ASSETS)
			{
				AssetManager.addFromXML( AssetManager.getXML(name) );
				AssetManager.remove(name);
			}
			
			StringManager.initialize();
			StringManager.addFromXML(AssetManager.getXML("locale"));
			
			_assetLoader = AssetManager.load();
			if (_assetLoader)
			{
				_assetLoader.addEventListener(Event.COMPLETE, onAssetLoaded);
				_assetLoader.load();
			}
			else
				onAssetLoaded(null);
		}
		
		private function onAssetLoaded(e:Event):void 
		{
			if (_assetLoader)
			{
				_assetLoader.removeEventListener(Event.COMPLETE, onAssetLoaded);
				_assetLoader = null;
			}
			
			//AppText.load();
			AppData.initialize();
			
			var suit:SuitApp = new SuitApp();
			addChild(suit);
			
			setChildIndex(_console, numChildren - 1);
		}
	}
}